﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.ServiceLocation;
using AzureGames.Model;
using AzureGames.Data.Implementation.ServiceEntities;

namespace AzureGames.Data.Implementation
{
    public class AIPlayerRepository : AzureTableRepository<AIPlayerEntity>, IAIPlayerRepository
    {
        const string PartionKey = "Players";

        public AIPlayerRepository() : base("AIPlayer") 
        {
            AutoMapper.Mapper.CreateMap<Model.AIPlayer, ServiceEntities.AIPlayerEntity>()
                  .ForMember(m => m.PartitionKey, s => s.UseValue("Players"))
                  .ForMember(m => m.RowKey, s => s.MapFrom(g => g.ID));
            AutoMapper.Mapper.CreateMap<ServiceEntities.AIPlayerEntity, Model.AIPlayer>();
        }

        public List<Model.AIPlayer> GetAIPlayers(int gameID)
        {
            return dataContext.CreateQuery<AIPlayerEntity>(this.tableName)
                .Where(n => n.PartitionKey == PartionKey && n.GameDefinitionID == gameID)
                .Cast<Model.AIPlayer>()
                .ToList();
        }

        public AzureResult AddAIPlayer(AIPlayer player)
        {
            return Add(player);
        }

        public AzureResult UpdateAIPlayer(AIPlayer player)
        {
            return Update(player);
        }       

        public AIPlayer GetAIPlayer(string playerID)
        {
            return Get(PartionKey, playerID);
        }
    }
}
